from django.conf import settings
from django.contrib.auth.models import AbstractBaseUser, AbstractUser
from django.db import models


class AgentInstitution(models.Model):
    """
    组织机构
    """
    name = models.CharField('公司名称', max_length=100)
    address = models.CharField('公司地址', max_length=300)
    code = models.CharField('公司营业编号', null=True, blank=True, max_length=50)
    licence_pic = models.ImageField('公司执照', upload_to='./institution')
    legal_person = models.CharField('公司法人', max_length=20)
    telephone = models.CharField('联系电话', max_length=100)
    email = models.CharField('公司邮箱', max_length=100)
    info = models.TextField('公司简介', max_length=4000)

    def __str__(self):
        return self.name


class User(AbstractUser):
    """
      用户
    """

    USER_TYPE_ADMIN = 1
    USER_TYPE_MANAGER = 2
    USER_TYPE_AGENT = 3
    USER_TYPE_CHOICES = ((USER_TYPE_MANAGER, '机构管理人'),
                         (USER_TYPE_AGENT, "经纪人"))
    SEX_MAN = 'M'
    SEX_WOMEN = 'F'
    SEX_CHOICES = ((SEX_MAN, '男'), (SEX_WOMEN, '女'))
    user_type = models.PositiveSmallIntegerField("用户类型", choices=USER_TYPE_CHOICES, default=USER_TYPE_AGENT)
    nick_name = models.CharField('昵称', null=True, blank=True, max_length=100)
    head_pic = models.ImageField('头像', upload_to='images',null=True)
    mobile_phone = models.PositiveIntegerField('手机号', null=True, blank=True, default=0)
    telephone = models.CharField('电话', null=True, blank=True, max_length=20)
    sex = models.CharField('姓别', max_length=1, choices=SEX_CHOICES, default=SEX_MAN)
    agent_institution = models.ForeignKey(AgentInstitution,null=True)

    def __unicode__(self):
        return self.user

    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'
